package com.zark.myspringboot.web;

import com.zark.myspringboot.web.listeners.ApplicationEnvironmentPreparedListener;
import com.zark.myspringboot.web.listeners.ApplicationFailedListener;
import com.zark.myspringboot.web.listeners.ApplicationPreparedListener;
import com.zark.myspringboot.web.listeners.ApplicationReadyListener;
import com.zark.myspringboot.web.listeners.ApplicationStartingListener;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

// same as @Configuration @EnableAutoConfiguration @ComponentScan
@SpringBootApplication(scanBasePackages = { "com.zark.myspringboot" })
@MapperScan(basePackages = "com.zark.myspringboot.**.mapper", annotationClass = org.springframework.stereotype.Repository.class)
// 启注解事务管理，等同于xml配置方式的 <tx:annotation-driven />
@EnableTransactionManagement
@ImportResource(locations = { "classpath:druid-spring.xml" })
@EnableCaching
public class MyspringbootApplication {

	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(MyspringbootApplication.class);
		// 是否允许命令行传入参数
		// app.setAddCommandLineProperties(false);
		app.addListeners(new ApplicationEnvironmentPreparedListener());
		app.addListeners(new ApplicationFailedListener());
		app.addListeners(new ApplicationPreparedListener());
		app.addListeners(new ApplicationReadyListener());
		app.addListeners(new ApplicationStartingListener());
		app.run(args);
	}

	@Bean
	public CommandLineRunner commandLineRunner(final ApplicationContext ctx) {
		return new CommandLineRunner() {

			@Override
			public void run(String... args) throws Exception {
				System.out.println("hot code");
				System.out.println("Let's inspect the beans provided by Spring Boot:");
				String[] beanNames = ctx.getBeanDefinitionNames();
				for (String beanName : beanNames) {
					if (!beanName.startsWith("org") && !beanName.startsWith("spring") && !beanName.startsWith("endpoints")) {
						System.out.println(beanName);
					}
				}
			}
		};
	}
}
